import { SET_LIST, DELETE_LIST, ADD_LIST, EDIT_LIST } from './actions'
export interface IAction {
	type: string
	payload: any
}

export interface IListItem {
	key: string
	id: string
	desc: string
	title: string
	url: string
	type: string
	type1: string
}


export interface IState {
	list: IListItem[],
	loading: boolean
}


const initState = {
	list: [],
	loading: false
}

const reducer = (state: IState = initState, action: IAction) => {
	switch (action.type) {
		case SET_LIST:
			return {
				...state,
				list: action.payload
			}
		case DELETE_LIST:
			return {
				...state,
				list: state.list.filter(v => v.id !== action.payload.id)
			}
		case ADD_LIST:
			return {
				...state,
				list: [action.payload, ...state.list]
			}
		case EDIT_LIST:
			return {
				...state,
				list: state.list.map(v => {
					if (v.id === action.payload.id) {
						Object.assign(v, action.payload)
					}
					return v
				})
			}
		case 'SET_LOADING': {
			return {
				...state,
				loading: action.payload
			}
		}
		default:
			return state
	}
}

export default reducer